package com.mrlee.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrlee.bean.Module;
import com.mrlee.bean.Role;
import com.mrlee.bean.RoleModule;
import com.mrlee.mapper.ModuleMapper;
import com.mrlee.mapper.RoleMapper;
import com.mrlee.mapper.RoleModuleMapper;
import com.mrlee.service.RoleService;

import com.mrlee.utils.AjaxResult;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/3/9.
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleModuleMapper roleModuleMapper;

    @Resource
    private ModuleMapper moduleMapper;

    /**
     * 查询全部角色
     * @return
     */
    @Override
    public AjaxResult selectAllRole() {
        AjaxResult result = new AjaxResult();
        List<Role> roleList = roleMapper.selectAllRole();
        if (roleList.isEmpty()) {
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(roleList);
        return result;
    }

    /**
     * 添加角色
     * @param role 角色信息
     * @param modules 权限集合
     * @return
     */
    @Override
    public AjaxResult addRole(Role role,List<Integer> modules) {

        AjaxResult result = new AjaxResult();
        if (role == null) {
            result.setCode("0");
            result.setMessage("添加失败");
            return result;
        }
        int count = roleMapper.insert(role);
        if (count==0){
            result.setCode("0");
            result.setMessage("添加失败");
            return result;
        }
        int id = role.getRoleId();

        for (int i = 0; i <modules.size() ; i++) {
            RoleModule roleModule = new RoleModule(id,modules.get(i));
            int countModule = roleModuleMapper.insert(roleModule);
            if (countModule == 0){
                result.setCode("0");
                result.setMessage("添加失败");
                return result;
            }
        }
        result.setCode("1");
        result.setMessage("添加成功");


        return result;
    }

    /**
     * 删除角色
     * @param roleId 角色Id
     * @return
     */
    @Override
    public AjaxResult deleteRole(int roleId) {
        AjaxResult ajaxResult = new AjaxResult();
        int result = roleModuleMapper.deleteByRoleId(roleId);
        if (result == 0){
            ajaxResult.setCode("0");
            ajaxResult.setMessage("删除失败");
            return ajaxResult;
        }
        int count = roleMapper.deleteByPrimaryKey(roleId);
        if (count == 0){
            ajaxResult.setCode("0");
            ajaxResult.setMessage("删除失败");
            return ajaxResult;
        }
        ajaxResult.setCode("1");
        ajaxResult.setMessage("删除成功");
        return ajaxResult;
    }

    /**
     * 修改角色
     * @param role 角色信息
     * @param modules 权限集合
     * @return
     */
    @Override
    public AjaxResult updateRole(Role role,List<Integer> modules) {
        AjaxResult result = new AjaxResult();
        if (role == null) {
            result.setCode("0");
            result.setMessage("修改失败");
            return result;
        }
        int count = roleMapper.updateByPrimaryKey(role);
        if (count==0){
            result.setCode("0");
            result.setMessage("修改失败");
            return result;
        }
        int id = role.getRoleId();

        roleModuleMapper.deleteByRoleId(id);
        for (int i = 0; i <modules.size() ; i++) {
            RoleModule roleModule = new RoleModule(id,modules.get(i));
            int countModule = roleModuleMapper.insert(roleModule);
            if (countModule == 0){
                result.setCode("0");
                result.setMessage("修改失败");
                return result;
            }
        }
        result.setCode("1");
        result.setMessage("修改成功");
        return result;
    }

    /**
     * 根据Id查找角色
     * @param roleId 角色Id
     * @return
     */
    @Override
    public Role selectRoleById(int roleId) {
        Role role = roleMapper.selectByPrimaryKey(roleId);
        List<Module> mList = role.getModuleList();
        for (int i = 0; i <mList.size() ; i++) {
            Module module = mList.get(i);
            int id = moduleMapper.findIdByName(module.getName());
            module.setModuleId(id);
        }
        role.setModuleList(mList);
        return role;
    }

    /**
     * 查询全部权限
     * @return
     */
    @Override
    public List<Module> selectAllModule() {
        List<Module> moduleList = moduleMapper.moduleList();
        for (int i = 0; i < moduleList.size(); i++) {
            Module module = moduleList.get(i);
            module.setModuleId(moduleMapper.findIdByName(module.getName()));
        }
        return moduleList;
    }

    /**
     * 分页
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Role> queryRoleByPage(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;

        //这句话一定写在查询之前
        PageHelper.startPage(pageNo,pageSize);
        List<Role> roleList = roleMapper.selectAllRole();
        PageInfo<Role> rolePageInfo = new PageInfo<Role>(roleList);
        return rolePageInfo;
    }

    /**
     * 根据管理员Id查询角色
     * @param adminid
     * @return
     */
    @Override
    public List<Role> selectRoleList(Integer adminid) {
        return roleMapper.selectRoleByAdminId(adminid);
    }


}
